package com.hobbit.analyze.analyzers
{
	import com.hobbit.analyze.base.Config;
	import com.hobbit.analyze.base.LoadItem;
	
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	
	/**
	 * Json格式文件解析器
	 */
	public class JsonAnalyze implements IAnalyze
	{
		public function JsonAnalyze()
		{
		}
		
		public function getDataList():Dictionary
		{
			return _jsonList;
		}
		
		
		private var _jsonList:Dictionary = new Dictionary();
		public function analyze(byteArray:ByteArray, loadItem:LoadItem, compFn:Function):void
		{
			byteArray.position = 0;
			var json:String = byteArray.readUTFBytes(byteArray.length);
			var jsonObj:Object = JSON.parse(json); /*com.adobe.serialization.json.JSON.decode(json)*/;
			loadItem.data = byteArray;
			loadItem.result = jsonObj;
			loadItem.status = LoadItem.STATUS_LOADED;
			_jsonList[loadItem.name] =jsonObj;
			if(loadItem.other == "serverid")
			{
				Config.getInstance().registerMatSheet(loadItem.result);
			}
			if(compFn!=null)
			{
				compFn(loadItem);
			}
		}
		
		/**
		 *	返回Object格式数据
		 * @param key 文件名或者serverId
		 * @param subKey 二级键值
		 * @return 
		 * 
		 */
		public function getData(key:String, subKey:String):*
		{
			var retObj:Object = _jsonList[key];
			if(retObj == null)
			{
				retObj = _jsonList[key + "$" + subKey];
			}
			return retObj;
		}
	}
}